package authenum

import (
	"yunj/pkg/yunj/core/enum"
)

// 定义页面打开类型常量
const (
	// 无
	PAGE_OPEN_NIL uint8 = 0

	// 子页面
	PAGE_OPEN_TAB uint8 = 11

	// 弹出层页面
	PAGE_OPEN_POPUP uint8 = 22

	// 浏览器新标签页
	PAGE_OPEN_NEW uint8 = 33
)

// 页面打开方式
// 实现EnumInerface接口
type PageOpen struct {
	enum.Enum[uint8]
}

// pageopentype.uint8.GetDescMap()
func (e *PageOpen) AllConstAttrs() map[uint8]map[string]any {
	return map[uint8]map[string]any{
		PAGE_OPEN_NIL: {
			"desc":   "无",
			"strVal": "",
			"color":  "#cccccc",
			"sort":   1,
		},
		PAGE_OPEN_TAB: {
			"desc":   "子页面",
			"strVal": "tab",
			"color":  "#16b777",
			"sort":   2,
		},
		PAGE_OPEN_POPUP: {
			"desc":   "弹出层页面",
			"strVal": "popup",
			"color":  "#16baaa",
			"sort":   3,
		},
		PAGE_OPEN_NEW: {
			"desc":   "浏览器新标签页",
			"strVal": "new",
			"color":  "#1e9fff",
			"sort":   4,
		},
	}
}

// instanceCache 存储已经创建的实例
var pageOpenInstanceCache = make(map[uint8]*PageOpen)

// 根据value获取枚举
func PageOpenByValue(value uint8) *PageOpen {
	if instance, exists := pageOpenInstanceCache[value]; exists {
		return instance
	}
	e := &PageOpen{}
	enum.SetEnumInstance(&e.Enum, e, value)
	pageOpenInstanceCache[value] = e
	return e
}

var PageOpenEnum *PageOpen = func() *PageOpen {
	e := &PageOpen{}
	enum.SetEnumInstance(&e.Enum, e)
	return e
}()

// 是否为内部页面
func (e *PageOpen) IsInnerPage() (res bool) {
	if e.Value == PAGE_OPEN_TAB || e.Value == PAGE_OPEN_POPUP {
		res = true
	}
	return
}
